//
//  VoiceChatStatusView.m
//  RainbowFM
//
//  Created by Kratos on 14/12/14.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "VoiceChatStatusView.h"

@interface VoiceChatStatusView ()
@property (nonatomic , strong) NSTimer *timer;
@end


@implementation VoiceChatStatusView

-(void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenStatusChanged:) name:kRFMNotificationVoiceChatScreenChanged object:nil];
}

-(void)setStatus:(VoiceChatStatus)status
{
    if (_status == status) {
        return;
    }
    _status = status;
    BOOL onlineImageHidden = NO;
    BOOL onlineTimeImageHidden = NO;
    BOOL onlineTimeLabelHidden = NO;
    
    NSString *info = @"";
    switch (status) {
        case VoiceChatStatusWaiting:
        {
            onlineImageHidden = YES;
            onlineTimeImageHidden = YES;
            onlineTimeLabelHidden = YES;
            info = @"等待中";
            
            [self stopTimer];
        }
            break;
        case VoiceChatStatusTimeout:
        {
            onlineImageHidden = YES;
            onlineTimeImageHidden = YES;
            onlineTimeLabelHidden = YES;
            info = @"连接超时";
            [self stopTimer];
            
        }
            break;
        case VoiceChatStatusOnlining:
        {
            info = @"通话中";
            [self startTimer];
            
        }
            break;
        case VoiceChatStatusOutofline:
        {
            onlineImageHidden = YES;
            onlineTimeImageHidden = YES;
            onlineTimeLabelHidden = YES;
            info = @"断开连接";
            [self stopTimer];
        }
            break;
        default:
            break;
    }
    
    typeof(self) self_ = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self_) {
            [self_.chatOnlineImageView setHidden:onlineImageHidden];
            [self_.chatOnlineTimeImageView setHidden:onlineTimeImageHidden];
            [self_.onlineTimeLabel setHidden:onlineTimeLabelHidden];
            
            [self_.statusLabel setText:info];
        }
    });
}

-(void)setScreenStatus:(VoiceChatScreenStatus)screenStatus
{
    if (_screenStatus == screenStatus) {
        return;
    }
    _screenStatus = screenStatus;
    
    NSString *bgImageName = @"";
    NSString *onlineImageName = @"";
    switch (_screenStatus) {
        case VoiceChatScreenStatusBan:
        {
            bgImageName = @"chat_b_stuta_bg@3x.png";
            onlineImageName = @"chat_b_online@3x.png";
        }
            break;
        case VoiceChatScreenStatusFull:
        {
            bgImageName = @"chat_f_stuta_bg@3x.png";
            onlineImageName = @"chat_f_online@3x.png";
        }
            break;
        default:
            break;
    }
    
    typeof(self) self_ = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self_) {
            [self_.backgroundImageview setImage:[UIImage imageNamed:bgImageName]];
            [self_.chatOnlineImageView setImage:[UIImage imageNamed:onlineImageName]];
        }
    });
}

-(void)screenStatusChanged:(NSNotification *)noti
{
    NSDictionary *status = noti.userInfo;
    self.screenStatus = [status[@"status"] integerValue];
}

-(void)startTimer
{
    [self stopTimer];
    
    [_onlineTimeLabel setText:@"00:00:00"];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f
                                              target:self
                                            selector:@selector(timerUpdate:)
                                            userInfo:nil
                                             repeats:YES];
}

-(void)stopTimer
{
    if (_timer && ![_timer isValid]) {
        [_timer invalidate];
    }
}

- (void)timerUpdate:(NSTimer *)timer
{
    NSInteger time;
    
    time ++;
    
    NSInteger hour = time / 60 / 60;
    NSInteger minute = (time - hour * 60 * 60) / 60;
    NSInteger second = time - hour * 60 * 60 - minute * 60;
    
    [_onlineTimeLabel setText:_F(@"%02ld:%02ld:%02ld",hour,minute,second)];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
